﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hospital_ChooseHosPurchaseListTra : System.Web.UI.Page,HZ.MBSM.BLL.TraDes
{
    Int64 CHPlistTraProject;
    Int64 AccountId_YY;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            String SupplyGoodsId, StrSql;
            CHPlistTraProject = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["ProjectId"]));

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                LBSuccess.Text = "";
                AccountId_YY = SysFun.ToLong(_Account.AccountId);
                if (!IsPostBack)
                {
                    Db db1 = new Db();
                    System.Data.IDataReader dr1;
                    String IsGoing = "";
                    String Sql = "Select Count(*) As cun From SupplyPlan_Goods Where ProjectId='" + CHPlistTraProject.ToString() + "'";
                    dr1 = db1.DataReader(Sql);
                    while (dr1.Read())
                    {
                        IsGoing = dr1["cun"].ToString();
                    }
                    dr1.Close();
                    dr1.Dispose();
                    db1.Close();
                    db1 = null;
                    if (IsGoing.Equals("0"))
                    {
                        Response.Write("<script>window.alert('还没有开始交易!');</script>");
                        Response.Write("<script>window.location='TraProject.aspx';</script>");
                        return;
                    }
                }

                if (Request.Form["PurchaseChoose"] == "确定选取" && Request.Form["chk"] != null)
                {
                    String Num = "";
                    LBSuccess.Text = "";
                    SupplyGoodsId = Request.Form["chk"];
                    Db db = new Db();
                    System.Data.IDataReader dr;
                    //是否已在医院采购目录中了
                    StrSql = "Select Count(*) As Cun From HospitalPurchase Where AccountId_YY='" + AccountId_YY.ToString() + "' And SupplyGoodsId In (" + SupplyGoodsId +") And ProjectId="+ CHPlistTraProject.ToString();
                    dr = db.DataReader(StrSql);
                    while (dr.Read()) 
                    {
                        Num = dr["cun"].ToString();
                    }
                    dr.Close();
                    dr.Dispose();
                    if (Num != "0")
                    {
                        db.Close();
                        Response.Write("<script>window.alert('对不起，该目录您已经在您医院的清单中了！');</script>");
                    }
                    else
                    {
                        string[] SGID = SupplyGoodsId.Length == 0 ? new string[] { "" } : SupplyGoodsId.Substring(0).Split(new char[] { ',' });
                        StrSql = "";
                        string _strAccountId_PS, _strBidPrice, _strGoodsState;
                        for (int i = 0; i < SGID.Length; i++)
                        {
                            
                            _strAccountId_PS = SysFun.ToTrim(Request.Form["AccountId_PS" + SGID[i]]);
                            _strBidPrice = SysFun.ToTrim(Request.Form["BidPrice" + SGID[i]]);
                            _strGoodsState = SysFun.ToTrim(Request.Form["GoodsState" + SGID[i]]);
                            if (_strGoodsState == "备案采购")
                            {
                                _strBidPrice = "0";
                            }
                            //071212 马HospitalPurchase 里的配送企业是分配送企业 此界面的主配送企业故不能添加
                             StrSql += "@" + "Insert Into HospitalPurchase (SupplyGoodsId,ProjectId,AccountId_YY,PurchasePrice) Values(" +
                               SGID[i].ToString() + "," + CHPlistTraProject.ToString() + "," + AccountId_YY.ToString() + ",'" + _strBidPrice + "') ";
                            //if (_strAccountId_PS == "")
                            //{
                            //    StrSql += "@" + "Insert Into HospitalPurchase (SupplyGoodsId,ProjectId,AccountId_YY,PurchasePrice) Values(" +
                            //        SGID[i].ToString() + "," + CHPlistTraProject.ToString() + "," + AccountId_YY.ToString() + ",'" + _strBidPrice + "') ";
                            //}
                            //else
                            //{
                            //    StrSql += "@" + "Insert Into HospitalPurchase (SupplyGoodsId,ProjectId,AccountId_PS,AccountId_YY,PurchasePrice) Values(" +
                            //        SGID[i].ToString() + "," + CHPlistTraProject.ToString() + "," + _strAccountId_PS + "," + AccountId_YY.ToString() + ",'" + _strBidPrice + "') ";
                            //}
                        }
                        if (StrSql.Length > 0)
                        {
                            db.ExecuteTransaction(StrSql.Substring(1).Split(new char[] { '@' }), 30);

                            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "HospitalPurchase", StrSql, "../Hospital/ChooseHosPurchaseListTra.aspx");
                            LBSuccess.Visible = true;
                            LBSuccess.Text = "选取成功";
                        }
                        db.Close(); 
                    }
                }
                showGridView();
            }
        }

    }

    public void showGridView()
    {

        string priceTitle = "挂网价";
        string field_lsj = "";
        string field_name = "";

        switch (CHPlistTraProject.ToString())
        {
            case "8":
                field_name = "最高零售价";
                field_lsj = ",Convert(numeric(20,2),RetailPrice) as " + field_name; 
                break;
            case "9":
                priceTitle = "最高零售价";
                break;
            case "10":
                priceTitle = "最高限价";
                
                field_name = "最高零售价";
                field_lsj = ",Convert(numeric(20,2),RetailPrice) as " + field_name;
                
                break;
            case "11":
                
            case "12":
                priceTitle = "最高零售价";
                break;

        }

        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods";                                               //要查询的表明
        dbPage.Key = "SupplyGoodsId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "SupplyGoodsId,GoodsId,ProductCode,ProductName,MedicineModel,Outlookc,ProjectId,Factor,CompanyName_SC,AccountId_TB,CompanyName_TB,CompanyName_PS,Accountid_PS,Convert(numeric(20,2),BidPrice) As BidPrice,case GoodsState when 0 then '原中标'  when 1 then '自行采购'  when 2 then '22种降价'  when 3 then '血液制品'  when 4 then '重点监控' end as GoodsState,UserCode_SC,UserCode_TB,UserCode_PS,GoodsName,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,QualityLevels As QualityLevels1,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,remark" + field_lsj;//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;                                                    //没有显示记录条数   
        dbPage.OrderBy = "ProductName,MedicineModel,Outlookc,SupplyGoodsId";//排序字段
        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='100%' border='0.5' align='left' cellpadding='2' cellspacing='1' class='table_table'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td    width='3%'>药品流水号</td>");
        strHtml.Append("<td   width='6%'style='display:none'>品目号</td>");
        strHtml.Append("<td   width='8%'>通用名</td>");
        strHtml.Append("<td    width='5%'>商品名</td>");
        strHtml.Append("<td    width='5%'>剂型</td>");
        strHtml.Append("<td    width='3%'>规格</td>");
        strHtml.Append("<td    width='5%'>转换系数</td>");
        strHtml.Append("<td   width='3%'>包装规格</td>");
        strHtml.Append("<td   width='6%'>质量层次</td>");



        strHtml.Append("<td    width='6%'>"+priceTitle+"</td>");
        if (field_lsj != "")
        {
            strHtml.Append("<td    width='6%'>" + field_name + "</td>");
        }

        strHtml.Append("<td   width='6%'>投标人名称</td>");
        strHtml.Append("<td   width='9%'>生产企业</td>");
        strHtml.Append("<td    width='6%'>生产企业注册号</td>");
        strHtml.Append("<td    width='6%'>主配送企业</td>");
        strHtml.Append("<td    width='6%'>配送企业注册号</td>");
        strHtml.Append("<td    width='6%'>采购目录状态</td>");
        strHtml.Append("<td    width='6%'>备注</td>");
        strHtml.Append("<td  width='6%'>查看分配送企业</td>");
        strHtml.Append("<td   width='3%'>选择</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();

        while (dr.Read())
        {
            if (i == 0)
            {
                strHtml.Append("<tr class='table_td'>");
                i++;
            }
            else
            {
                strHtml.Append("<tr class='table_td2'>");
                i--;
            }
           
            strHtml.Append("<td width='3%'><a target='_blank' href='../Bidder/GoodDetail.aspx?Goodsid=" + dr["GoodsId"].ToString() + "'>" + dr["GoodsId"].ToString() + "</a></td>");
            strHtml.Append("<td width='6%' style='display:none'>" + dr["ProductCode"].ToString() + "</td>");
            strHtml.Append("<td width='8%'>" + dr["ProductName"].ToString() + "</td>");
            strHtml.Append("<td width='5%'>" + dr["GoodsName"].ToString() + "</td>");
            strHtml.Append("<td width='5%'>" + dr["Medicinemodel"].ToString() + "</td>");
            strHtml.Append("<td width='3%'>" + dr["Outlookc"].ToString() + "</td>");
            //strHtml.Append("<td nowrap><a href='../Bidder/GoodDetail.aspx?Goodsid=" + dr["GoodsId"].ToString() + "'>商品详细</a></td>");
            strHtml.Append("<td width='5%'>" + dr["Factor"].ToString() + "</td>");
            strHtml.Append("<td width='3%'>" + dr["Pack1"].ToString() + "</td>");
            strHtml.Append("<td width='6%'>" + dr["QualityLevels"].ToString() + "</td>");
            string _strGoodsState = dr["GoodsState"].ToString();
            switch (_strGoodsState)
            {
                case "原中标":
                    strHtml.Append("<td width='6%'><input type='hidden' name='GoodsState" + dr["SupplyGoodsId"].ToString() + "' value='原中标' /><input type='hidden' name='BidPrice" + dr["SupplyGoodsId"].ToString() + "' value='" + dr["BidPrice"].ToString() + "' />" + dr["BidPrice"].ToString() + "</td>");
                    break;
                case "自行采购":
                    strHtml.Append("<td width='6%'><input type='hidden' name='GoodsState" + dr["SupplyGoodsId"].ToString() + "' value='自行采购' /><input type='hidden' name='BidPrice" + dr["SupplyGoodsId"].ToString() + "' value='" + dr["BidPrice"].ToString() + "' />" + dr["BidPrice"].ToString() + "</td>");
                    break;
                case "22种降价":
                    strHtml.Append("<td width='6%'><input type='hidden' name='GoodsState" + dr["SupplyGoodsId"].ToString() + "' value='22种降价' /><input type='hidden' name='BidPrice" + dr["SupplyGoodsId"].ToString() + "' value='" + dr["BidPrice"].ToString() + "' />" + dr["BidPrice"].ToString() + "</td>");
                    break;
                case "血液制品":
                    strHtml.Append("<td width='6%'><input type='hidden' name='GoodsState" + dr["SupplyGoodsId"].ToString() + "' value='血液制品' /><input type='hidden' name='BidPrice" + dr["SupplyGoodsId"].ToString() + "' value='" + dr["BidPrice"].ToString() + "' /></td>");
                    break;
                case "重点监控":
                    strHtml.Append("<td width='6%'><input type='hidden' name='GoodsState" + dr["SupplyGoodsId"].ToString() + "' value='重点监控' /><input type='hidden' name='BidPrice" + dr["SupplyGoodsId"].ToString() + "' value='" + dr["BidPrice"].ToString() + "' />" + dr["BidPrice"].ToString() + "</td>");
                    break;

            }

            if (field_lsj != "")
            {
                strHtml.Append("<td    width='6%'>" + dr[field_name].ToString() + "</td>");
            }

            strHtml.Append("<td width='6%'><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_TB"].ToString() + "' target='_blank'>" + dr["CompanyName_TB"].ToString() + "</a></td>");
 //           strHtml.Append("<td style='display:none' width='6%'>" + dr["UserCode_TB"].ToString() + "</td>");
            strHtml.Append("<td width='6%'>" + dr["CompanyName_SC"].ToString() + "</td>");
            strHtml.Append("<td width='6%'>" + dr["UserCode_SC"].ToString() + "</td>");
            strHtml.Append("<td width='6%'><input type='hidden' name='AccountId_PS" + dr["SupplyGoodsId"].ToString() + "' value='" + dr["Accountid_PS"].ToString() + "' /><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_PS"].ToString() + "' target='_blank'>" + dr["CompanyName_PS"].ToString() + "</a> </td>");
            strHtml.Append("<td width='6%'>" + dr["UserCode_PS"].ToString() + "</td>");
            strHtml.Append("<td width='6%'>" + dr["GoodsState"].ToString() + "</td>");
            strHtml.Append("<td width='6%'>" + dr["remark"].ToString() + "</td>");
           // if (dr["isselect"].ToString() == "")
           // {
            //    strHtml.Append("<td width='6%'><fount color='red'>无配送企业</fount></td>");
           // }
           // else
           // {
                strHtml.Append("<td width='6%'><a href='search_company_psnext.aspx?SupplyGoodsId=" + dr["SupplyGoodsId"].ToString() + "&ProjectId=" + dr["ProjectId"].ToString() + "&Accountid_PS=" + dr["Accountid_PS"].ToString() + "' target='_blank' >查看分配送企业</a></td>");
            //}
                strHtml.Append("<td width='3%'><input name='chk' type='checkbox' value='" + dr["SupplyGoodsId"].ToString() + "' /></td>");
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();

      
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
      
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
        //this.DbPageFlip2.DbPage = dbPage;


    }

    public string fctConditions()
    {
        string strtemp,strtemp1, GoodsState, Status, UserCode_SC, CompanyName_SC, _GoodsId, ProudctName, Outlookc, ProductCode,GoodsName,CompanyName_TB,UserCode_TB;

        //药品查询范围
        Status = SysFun.ToTrim(DLStatus.SelectedValue);
        strtemp = " isuse=1 and ProjectId=" + CHPlistTraProject.ToString() + " And";
        strtemp1 = " ProjectId=" + CHPlistTraProject.ToString() + " And  isuse=1 and AccountId_YY=" + AccountId_YY.ToString() + " ";
       

        //采购目录状态
        GoodsState = SysFun.ToTrim(DLGoods.SelectedValue);
        switch (GoodsState)
        {
            case "全部":
                  break;
            case "原中标商品":
                strtemp += " GoodsState='0' And";
                break;
            case "备案采购商品":
                strtemp += " GoodsState='1' And";
                break;
            case "不限价不竞价商品":
                strtemp += " GoodsState='2' And";
                break;
 
        }

        //通用名或拼音码
        ProudctName = this.txtProudctName.Text.ToString();
        if (ProudctName != "")
        {
            strtemp += " (ProductName like '%" + ProudctName + "%' or Product_SpellingCode like '%" + ProudctName + "%') and";
        }
        
        //规格
        Outlookc = this.txtOutLookc.Text.ToString();
        if (Outlookc != "")
        {
            strtemp += " outlookc like '%" + Outlookc + "%' and";
        }

        //流水号
        _GoodsId = SysFun.ToTrim(this.txtGoodsId.Text);
        if (_GoodsId != "")
        {
            strtemp += " GoodsId = " + _GoodsId + " and";
        }

        //生产企业名称
        CompanyName_SC = this.txtCompanyName_SC.Text.ToString();
        if (CompanyName_SC != "")
        {
            strtemp += " CompanyName_SC like '%" + CompanyName_SC + "%' and";
        }

        //生产企业编码
        UserCode_SC = this.txtUserCode_SC.Text.ToString();
        if (UserCode_SC != "")
        {
            strtemp += " UserCode_SC like '%" + UserCode_SC + "%' and";
        }

        ////品目号
        //ProductCode = this.txtProductCode.Text.ToString();
        //if (ProductCode != "")
        //{
        //    strtemp = " ProductCode like '%" + ProductCode + "%' and";
        //}

        //商品名
        GoodsName = this.txtGoodsName.Text.ToString();
        if (GoodsName != "")
        {
            strtemp += " GoodsName like '%" + GoodsName + "%' and";
        }

        //投标人名称
        CompanyName_TB = this.txtCompanyName_TB.Text.ToString();
        if (CompanyName_TB != "")
        {
            strtemp += " CompanyName_TB like '%" + CompanyName_TB + "%' and";
        }


        //投标人编码
        UserCode_TB = this.txtUserCode_TB.Text.ToString();
        if (UserCode_TB != "")
        {
            strtemp += " UserCode_TB like '%" + UserCode_TB + "%' and";
        }
        switch (Status)
        {
            case "1"://不在医院采购目录的配送目录
                strtemp += " SupplyGoodsId Not In (Select Distinct SupplyGoodsId From HospitalPurchase Where " + strtemp1 + ") And";
                break;
            case "2"://全部挂网配送目录表的配送目录
                break;
            case "3"://已选择的挂网药品
                strtemp += " SupplyGoodsId  In (Select Distinct SupplyGoodsId From HospitalPurchase Where " + strtemp1 + ") And";
                break;
        }
        if (strtemp != "")
        {
            strtemp =  strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;

    }
    protected void ToExcul_Click(object sender, EventArgs e)//导出到Excel
    {
        string priceTitle = "挂网价";
        string field_lsj = "";
        string field_name = "";
        switch (CHPlistTraProject.ToString())
        {
            case "8":
                field_name = "最高零售价";
                field_lsj = ",RetailPrice as " + field_name;
                break;
            case "9":
                priceTitle = "最高零售价";
                break;
            case "10":
                priceTitle = "最高限价";
                field_name = "最高零售价";
                field_lsj = ",RetailPrice as " + field_name;

                break;
            case "11":
                break;

        }

        string StrSqlsele = "select GoodsId as 药品流水号,ProductCode as 品目号,ProductName as 通用名,GoodsName as 商品名,MedicineModel as 剂型,Outlookc as 规格,Factor as 转换系数,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As 包装规格,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS 质量层次 , Convert(numeric(20,2),BidPrice)  AS  " + priceTitle + field_lsj + ",CompanyName_TB as 投标人名称,UserCode_TB as 投标人注册号,CompanyName_SC as 生产企业,UserCode_SC as 生产企业注册号,CompanyName_Ps as 配送企业,UserCode_PS as 配送企业注册号,case GoodsState when 0 then '原中标'  when 1 then '自行采购'  when 2 then '22种降价'  when 3 then '血液制品'  when 4 then '重点监控' end as 采购目录状态,remark as 备注 from SupplyPlan_Goods where  IsUse=1 and ProjectId=" + CHPlistTraProject.ToString();
        Db db = new Db();
        System.Data.IDataReader dr;
        dr = db.DataReader(StrSqlsele);
        dv.DataSource = dr;
        dv.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        Print();

    }
    public void Print()
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode( "挂网目录.xls"));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.dv.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        Response.Write(oStringWriter.ToString());
        // 停止页面的执行
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    } 

    #region TraDes 成员

    public string Name
    {
        get { return "选择采购药品"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "交易数据管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;
        }
    }

    #endregion
    
}
